package archer.back.models

import java.util.Date

import io.circe.generic.JsonCodec
import io.circe.{ Decoder, Encoder, ObjectEncoder }

case class CommonBody(
  url: String,
  contentType: Option[String],
  referer: Option[String],
  body: Option[String] = Option.empty,
  textBody: Option[String] = Option.empty,
  law_body: Map[String, String] = Map.empty,
  law_text_body: Map[String, String] = Map.empty,
  cssBody: Option[String] = Option.empty,
  host: Option[String],
  protocol: Option[String],
  deep: Int,
  wasteTime: Long,
  requestTime: Date,
  status: Int,
  contentLength: Int,
  subs: List[SubUrl] = List.empty,
  planId: Int,
  actionId: Int)

object CommonBody {
  import io.circe.generic.extras.auto._
  import io.circe.generic.extras.Configuration

  implicit val configure = Configuration.default.withDefaults

  implicit val DatetEncoder: Encoder[Date] = {
    Encoder.encodeLong.contramap[Date](_.getTime)
  }

  implicit val DateDecoder: Decoder[Date] = {
    Decoder.decodeLong.map(s => new Date(s))
  }

  implicit val CommonBodyObjectEncoder: ObjectEncoder[CommonBody] = {
    exportEncoder.instance
  }

  implicit val CommonBodyDecoder: Decoder[CommonBody] = {
    exportDecoder.instance
  }

  def simpleToString(common: CommonBody): String = {
    common.copy(body = Option.empty).toString
  }
}

@JsonCodec
case class SubUrl(url: String, path: List[Int], targetClass: String)
